# -*- coding: utf-8 -*-
import wx
import os
import re


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Rename GUI", size=(500, 400))
        self.path = ''
        panel = wx.Panel(self)
        inner = wx.BoxSizer(wx.HORIZONTAL)
        text = wx.StaticText(panel, -1, "PATH:")
        self.tx_line = wx.TextCtrl(panel, -1, '')
        ok_btn = wx.Button(panel, -1, "rename")
        ok_btn.Bind(wx.EVT_BUTTON, self.rename)
        inner.Add(text, 0, wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 15)
        inner.Add(self.tx_line, 1, wx.LEFT, 15)
        inner.Add(ok_btn, 0, wx.LEFT | wx.RIGHT, 15)
        line = wx.StaticLine(panel, -1, size=(-1, -1), style=wx.LI_HORIZONTAL)

        self.tc = wx.TextCtrl(panel, -1, "", style=wx.TE_RICH | wx.TE_MULTILINE | wx.TE_READONLY)
        main = wx.BoxSizer(wx.VERTICAL)
        main.Add(inner, 0, wx.EXPAND | wx.TOP, 20)
        main.Add(line, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 10)
        main.Add(self.tc, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 15)
        panel.SetSizer(main)
        self.Center()
        self.Show()

    def rename(self, evt):
        self.path = self.tx_line.GetValue()
        pattern = '[.]?\S\d{1,3}\E\d{1,3}'
        f = os.listdir(self.path)
        self.tc.Clear()
        for i, name in enumerate(f):
            self.tc.AppendText(str(i + 1) + ': ' + name + ' --------------------------------> ')
            ls = os.path.splitext(name)
            another_ls = re.split(pattern, ls[0])
            new_name = ls[0].replace(another_ls[1], '') + ls[1]
            os.rename(self.path + name, self.path + new_name)
            self.tc.AppendText(str(i + 1) + ': ' + new_name + ' [successful] \n\n')

app = wx.PySimpleApp()
frm = MyFrame()
frm.Show()
app.MainLoop()